<link rel="stylesheet" href="/static/css/system/systemUser.css" type="text/css">
<div class="select-div">
    搜索用户：
    <div class="layui-inline">
        <input class="layui-input" name="keyword" id="demoReload" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>
<table id="users" lay-filter="users"></table>
<script>
    layui.use('table', function(){
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#users',
            url: '/systemUser/getUserList', //数据接口
            method: 'post',
            page: true, //开启分页
            cols: [[ //表头
                {field: 'id', title: 'ID', fixed: 'left'}
                ,{field: 'name', title: '名字' }
                ,{field: 'username', title: '用户名'}
                ,{field: 'roleName', title: '角色'}
                ,{field: 'mobile', title: '手机号码'}
                ,{field: 'email', title: '邮箱',}
                ,{field: 'createTime', title: '创建时间',
                    templet: function(d){
                        return new Date(d.createTime).Format('yyyy-MM-dd hh:mm:ss');
                    }
                },
                {field:'right', title: '操作', width:177,toolbar:"#add-role"}
            ]]
        });

        //监听工具条
        table.on('tool(users)', function(obj){
            var data = obj.data;
            var roleId = data.roleId;
            layer.open({
                title: '选择角色' ,
                area: ['150px', '150px'],
                content:

                        '<div class="role-select"><select id="role_select_id">'+
                            '<% for(role in roles){ %>'+
                                '<option value="${role.id}">${role.name}</option>'+
                            '<% } %>'+
                        '</select></div>' ,
                yes:function () {debugger
                    var selectRoleId =  $("#role_select_id").val();
                    $.ajax({
                        type: "POST",
                        url: "/systemUser/updateUserRole",
                        data: {"userId": data.id, "roleId":selectRoleId},
                        success: function (data) {
                            if (data != null && data.code == 200) {
                                layer.msg(data.msg);
                                setTimeout(function () {
                                    forwardByUrlAndTitle("/systemUser/index", "用户管理");
                                }, 1000)
                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });
                }
            });
        });
    });

</script>
<script type="text/html" id="add-role">
    <a class="layui-btn layui-btn-xs" lay-event="edit-role" >管理角色</a>
</script>